package com.lc.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.reggie.dto.SetmealDto;
import com.lc.reggie.entity.Category;
import com.lc.reggie.entity.Setmeal;
import com.lc.reggie.service.CategoryService;
import com.lc.reggie.service.SetmealDishService;
import com.lc.reggie.service.SetmealService;
import com.lc.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController
{
	@Autowired
	private SetmealService setmealService;
	
	@Autowired
	private SetmealDishService setmealDishService;
	
	@Autowired
	private CategoryService categoryService;
	
	@PostMapping
	@CacheEvict(value = "setmealCache", allEntries = true)
	public R<String> save(@RequestBody SetmealDto setmealDto)
	{
		log.info("套餐信息：{}",setmealDto);
		
		setmealService.saveWithDish(setmealDto);
		
		return R.success("新增套餐成功");
	}
	
	
	/**
	 * 套餐分页查询
	 * @param page
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@GetMapping("/page")
	public R<Page> getPage(int page, int pageSize, String name)
	{
		//分页构造器对象
		Page<Setmeal> pageInfo = new Page<>(page, pageSize);
		Page<SetmealDto> setmealDtoPage = new Page<>();
		
		LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
		//添加查询条件，根据name进行like模糊查询
		lqw.like(name != null, Setmeal::getName, name);
		//添加排序条件，根据更新时间降序排列
		lqw.orderByDesc(Setmeal::getUpdateTime);
		//执行分页查询
		setmealService.page(pageInfo, lqw);
		
		//对象拷贝
		BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");
		List<Setmeal> records = pageInfo.getRecords();
		
		List<SetmealDto> list = records.stream().map((item) -> {
			SetmealDto setmealDto = new SetmealDto();
			//对象拷贝
			BeanUtils.copyProperties(item, setmealDto);
			//分类id
			Long categoryId = item.getCategoryId();
			//根据分类id查询分类对象
			Category category = categoryService.getById(categoryId);
			if (category != null)
			{
				//分类名称
				String categoryName = category.getName();
				setmealDto.setCategoryName(categoryName);
			}
			
			return setmealDto;
		}).collect(Collectors.toList());
		
		setmealDtoPage.setRecords(list);
		
		return R.success(setmealDtoPage);
	}
	
	/**
	 * 删除套餐
	 * @param ids
	 * @return
	 */
	@DeleteMapping
	@CacheEvict(value = "setmealCache", allEntries = true)
	public R<String> delete(@RequestParam List<Long> ids)
	{
		log.info("ids: {}", ids);
		
		setmealService.removeWithDish(ids);
		
		return R.success("套餐数据删除成功");
	}
	
	/**
	 * 回显套餐数据
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<SetmealDto> getById(@PathVariable Long id)
	{
		log.info("id: {}", id);
		
		SetmealDto setmealDto = setmealService.getByIdWithDish(id);
		
		return R.success(setmealDto);
	}
	
	/**
	 * 修改套餐信息
	 * @param setmealDto
	 * @return
	 */
	@PutMapping
	@CacheEvict(value = "setmealCache", allEntries = true)
	public R<String> update(@RequestBody SetmealDto setmealDto)
	{
		setmealService.updateWithDish(setmealDto);
		return R.success("修改套餐成功");
	}
	
	/**
	 * 停售或起售套餐
	 */
	@PostMapping("/status/{status}")
	@CacheEvict(value = "setmealCache", allEntries = true)
	public R<String> stopOrStartSell(@PathVariable Integer status, @RequestParam List<Long> ids)
	{
		log.info("status: {}", status);
		
		setmealService.stopOrStartSell(status, ids);
		
		return R.success(status == 0 ? "停售成功" : "起售成功");
	}
	
	/**
	 * 根据条件查询套餐数据
	 * @param setmeal
	 * @return
	 */
	@GetMapping("/list")
	@Cacheable(value = "setmealCache", key = "#setmeal.categoryId + '_' + #setmeal.status")
	public R<List<Setmeal>> getList(Setmeal setmeal)
	{
		LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
		Long categoryId = setmeal.getCategoryId();
		Integer status = setmeal.getStatus();
		lqw.eq(categoryId != null, Setmeal::getCategoryId, categoryId);
		lqw.eq(status != null, Setmeal::getStatus, status);
		lqw.orderByDesc(Setmeal::getUpdateTime);
		List<Setmeal> setmealList = setmealService.list(lqw);
		return R.success(setmealList);
	}
}
